package com.mcxx.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mcxx.modules.minimumliving.service.SalvationApplyService;
import com.mcxx.modules.miniSalvation.family.dto.MemberAssetsDTO;
import com.mcxx.modules.miniSalvation.family.entity.MemberAssetsEntity;
import com.mcxx.modules.miniSalvation.family.service.read.MemberAssetsReadService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.validate.group.Creation;
import com.mcxx.validate.group.Update;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@Api("家庭财产收入控制类")
@RestService({"salvation/memberAssets/v2"})
public class MemberAssetsController extends DefaultRestService {

    private final static Logger logger = LoggerFactory.getLogger(MemberAssetsController.class);

    @Autowired
    private SalvationApplyService salvationApplyService;
    @Autowired
    private MemberAssetsReadService memberAssetsReadService;




    @GetMapping("getMemberAssetsList")
    @ApiOperation("根据familyId获取员财产收入列表")
    public void getMemberAssetsList(@V({"notnull"}) String familyId, @V({"notnull"}) String orderId) {
        logger.info("根据familyId获取员财产收入列表，参数是：{}", familyId);
        List<MemberAssetsDTO> list = memberAssetsReadService.queryListByFamilyId(familyId, orderId, "03");
        setData(list);
        logger.info("根据familyId获取员财产收入列表，结果是：{}", list);
    }

    @GetMapping("getMemberIncomeList")
    @ApiOperation("根据familyId获取成员财产收入列表")
    public void getMemberIncomeList(@V({"notnull"}) String familyId, @V({"notnull"}) String orderId, String type) {
        logger.info("根据familyId获取员财产收入列表，参数是：{}", familyId);
        Map<String,Object> list = memberAssetsReadService.getMemberIncomeList(familyId, orderId, type);
        setData(list);
        logger.info("根据familyId获取员财产收入列表，结果是：{}", list);
    }


    @GetMapping("getMemberAssetsById")
    @ApiOperation("根据id获取员财产收入列表")
    public void getMemberAssetsById(@V({"notnull"}) String id) {
        logger.info("根据id获取员财产收入列表，参数是：{}", id);
        MemberAssetsEntity entity = memberAssetsReadService.getById(id);
        setData(entity);
        logger.info("根据id获取员财产收入列表，结果是：{}", entity);
    }


    @PostMapping("addMemberAssets")
    @ApiOperation("新增成员财产收入")
    public void addMemberAssets(@Validated(Creation.class) MemberAssetsEntity memberAssetsEntity){
        logger.info("新增成员财产收入，参数：{}", JSON.toJSONString(memberAssetsEntity));
        double familyRevenue = salvationApplyService.saveMemberAssets(memberAssetsEntity, getCurUserAccount());
        setData(familyRevenue);
        logger.info("新增成员财产收入，家庭人均月收入结果是：{}", familyRevenue);
    }


    @PostMapping("addMemberIncome")
    @ApiOperation("新增成员财产收入")
    public void addMemberIncome(@RequestBody MemberAssetsDTO dto){
        logger.info("新增成员财产收入，参数：{}", JSON.toJSONString(dto));
        List<JSONObject> list = (List<JSONObject>)dto.getMemberList();
        double familyRevenue = salvationApplyService.saveMemberIncome(list,dto.getOrderId(),dto.getFamilyId(),getCurUserAccount(),dto);

        setData(familyRevenue);
        logger.info("新增成员财产收入，家庭人均月收入结果是：{}", dto);
    }


    @PostMapping("modifyMemberAssets")
    @ApiOperation("修改成员财产收入")
    public void modifyMemberAssets(@Validated(Update.class) MemberAssetsEntity memberAssetsEntity){
        logger.info("修改成员财产收入，参数：{}", JSON.toJSONString(memberAssetsEntity));
        double familyRevenue = salvationApplyService.saveMemberAssets(memberAssetsEntity, getCurUserAccount());
        setData(familyRevenue);
        logger.info("修改成员财产收入，家庭人均月收入结果是：{}", familyRevenue);
    }


    @PostMapping("delMemberAssets")
    @ApiOperation("删除成员财产收入")
    public void delMemberAssets(@V({"notnull"}) MemberAssetsEntity memberAssetsEntity){
        logger.info("删除成员财产收入，参数：{}", memberAssetsEntity);
        double familyRevenue = salvationApplyService.deleteMemberAssets(memberAssetsEntity);
        setData(familyRevenue);
        logger.info("修改成员财产收入，家庭人均月收入结果是：{}", familyRevenue);
    }

}
